﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using Benefit.Models.Sys;

namespace Benefit.Service.Day
{
    /// <summary>
    /// 小组日累计
    /// </summary>
    public class ServiceGroupDayChange : Benefit.Interface.Day.IGroupDayChange
    {
        Benefit.DB.DBManager db = null;
        public ServiceGroupDayChange(Benefit.DB.DBManager db)
        {
            this.db = db;
        }

        /// <summary>
        /// 删除当日所有记录
        /// </summary>
        /// <param name="tradeHistoryId"></param>
        public void Delete(int tradeHistoryId)
        {
            var query = from t in db.GroupDayChange where t.TradeHistoryId == tradeHistoryId select t;
            foreach (Models.Day.GroupDayChange c in query)
            {
                db.GroupDayChange.Remove(c);
            }
            db.SaveChanges();
        }

        /// <summary>
        /// 保存小组累计值
        /// </summary>
        /// <param name="tradeHistoryId">插入时间编号</param>
        /// <param name="pdate">插入时间</param>
        public void InitGroupDaySum(int tradeHistoryId, string pdate)
        {
            Interface.Sys.ITradeHistory ith = new Sys.ServiceTradeHistory(db);
            Interface.Sys.IDayGroupOperators idgo = new Sys.ServiceDayGroupOperators(db);
            Interface.Sys.IDayOperatorAccount ioa = new Sys.ServiceDayOperatorAccount(db);
            DateTime _pdate = ith.GetPdateFromTradeHistoryId(tradeHistoryId);
            List<Models.Sys.Groups> groups = db.Groups.ToList();
            foreach (Models.Sys.Groups g in groups)
            {
                Models.Day.GroupDayChange day = new Models.Day.GroupDayChange();
                day.GroupId = g.Id;
                day.TradeHistoryId = tradeHistoryId;
        
                List<Operator> operators = idgo.GetGroupOperators(g.Id, _pdate);

                List<Models.Day.AccountDayChange> change1 = new List<Models.Day.AccountDayChange>();


                foreach (Models.Sys.Operator oper in operators)
                {

                    List<Account> accounts = ioa.GetDayOperatorAccounts(_pdate, oper.Id);
                    foreach (Account acc in accounts)
                    {
                        var query1 = from t in db.AccountDayChange where t.AccountId == acc.Id && t.TradeHistoryId == tradeHistoryId select t;
                        if (query1.Count() > 0)
                        {
                            change1.Add(query1.First());
                        }
                    }
                }
                if (change1.Count > 0)
                {
                    day.BillCount = change1.Sum(a => a.BillCount);
                    day.DayCount = change1.Sum(a => a.DayCount);
                    day.Free = change1.Sum(a => a.Free);
                    day.LoseCount = Convert.ToInt32(change1.Sum(a => a.LoseCount));
                    day.Profit = change1.Sum(a => a.Profit);
                    day.SumLost = change1.Sum(a => a.SumLost);
                    day.SumWin = change1.Sum(a => a.SumWin);
                    day.UsedMargin = change1.Sum(a => a.UsedMargin);
                    day.WinCount = Convert.ToInt32(change1.Sum(a => a.WinCount));
                }

                List<Models.Day.AccountDayAnalysis> change2 = new List<Models.Day.AccountDayAnalysis>();

                foreach (Models.Sys.Operator oper in operators)
                {
                    List<Account> accounts = ioa.GetDayOperatorAccounts(_pdate, oper.Id);
                    foreach (Account acc in accounts)
                    {
                        var query2 = from t in db.AccountDayAnalysis where t.AccountId == acc.Id && t.TradeHistoryId == tradeHistoryId select t;
                        if (query2.Count() > 0)
                        {
                            change2.Add(query2.First());
                        }
                    }
                }
                if (change2.Count > 0)
                {
                    day.AvgBillBenefit = change2.Sum(a => a.AvgBillBenefit);
                    day.AvgPostionTime = change2.Sum(a => a.AvgPostionTime);
                    day.MaxReturn = change2.Sum(a => a.MaxReturn);
                    day.OverStopLoss = change2.Sum(a => a.OverStopLoss);
                    day.PingJinPing = Convert.ToInt32(change2.Sum(a => a.PingJinPing));
                    day.RaiseRate = change2.Sum(a => a.RaiseRate);
                    day.RiskCount = Convert.ToInt32(change2.Sum(a => a.RiskCount));
                    day.Utilization = change2.Sum(a => a.Utilization);
                    day.WinRate = change2.Sum(a => a.WinRate);
                    day.Yield = change2.Sum(a => a.Yield);
                }

                db.GroupDayChange.Add(day);
            }
            db.SaveChanges();
        }

        public Models.Day.GroupDayChange GetGroupDayChange(int groupId, int tradeHistoryId)
        {
            var query = db.GroupDayChange.Where(a => a.TradeHistoryId == tradeHistoryId).Where(a => a.GroupId == groupId);
            if (query.Count() > 0)
            {
                return query.First();
            }
            else
            {
                return null;
            }
        }
        public Models.Day.GroupDayChange GetGroupDayChange(int groupId, int day, int month, int year)
        {
            var query = db.GroupDayChange.Where(a => a.TradeHistory.DPdate == day).Where(a => a.TradeHistory.MPdate == month).Where(a => a.TradeHistory.YPdate == year).Where(a => a.GroupId == groupId).Include(a => a.Group);
            if (query.Count() > 0)
            {
                return query.First();
            }
            else
            {
                return null;
            }
        }
        public List<Models.Day.GroupDayChange> GetGroupDayChange(int groupId, int month, int year)
        {
            var query = db.GroupDayChange.Where(a => a.TradeHistory.YPdate == year).Where(a => a.TradeHistory.MPdate == month).Where(a => a.GroupId == groupId).Include(a => a.TradeHistory);
            if (query.Count() > 0)
            {
                return query.ToList();
            }
            else
            {
                return null;
            }
        }


        public List<Models.Day.GroupDayChange> GetCompanyDayChange(int day, int month, int year)
        {
            var query = db.GroupDayChange.Where(a => a.TradeHistory.YPdate == year).Where(a => a.TradeHistory.MPdate == month).Where(a => a.TradeHistory.DPdate == day).Include(a => a.Group);
            if (query.Count() > 0)
            {
                return query.ToList();
            }
            else
            {
                return null;
            }
        }
    }
}
